package com.uroad.liaoningpos.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.RemoteException;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.boc.aidl.printer.AidlPrinter;
import com.boc.aidl.printer.AidlPrinterListener;
import com.ums.AppHelper;
import com.ums.synthpayplugin.util.DeviceUtil;
import com.uroad.liaoningpos.R;
import com.uroad.liaoningpos.activity.MainActivity;
import com.uroad.liaoningpos.model.PayDetailMDL;
import com.uroad.liaoningpos.util.BitmapUtil;
import com.uroad.lib.util.file.SDUtil;
import com.uroad.lib.util.log.LogUtil;
import com.uroad.lib.util.string.StringUtils;
import com.uroad.lib.util.sys.SystemUtil;
import com.uroad.lib.widget.dialog.DialogHelper;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * @创建者 wlw
 * @创建时间 2019/4/3 10:14
 * @描述
 */
public class PayDetailDialog extends Dialog {
    private Context mContext;
    @BindView(R.id.llPrintContent)
    LinearLayout llPrintContent;
    @BindView(R.id.tvMerchantName)
    TextView tvMerchantName;
    @BindView(R.id.tvMerchantNo)
    TextView tvMerchantNo;
    @BindView(R.id.tvTerminalNo)
    TextView tvTerminalNo;
    @BindView(R.id.tvOperator)
    TextView tvOperator;
    @BindView(R.id.tvDocumentNo)
    TextView tvDocumentNo;
    @BindView(R.id.tvTransactionTime)
    TextView tvTransactionTime;
    @BindView(R.id.tvTransactionType)
    TextView tvTransactionType;
    @BindView(R.id.tvTransactionAmount)
    TextView tvTransactionAmount;

    @BindView(R.id.tvMerchantNameP)
    TextView tvMerchantNameP;
    @BindView(R.id.tvMerchantNoP)
    TextView tvMerchantNoP;
    @BindView(R.id.tvTerminalNoP)
    TextView tvTerminalNoP;
    @BindView(R.id.tvOperatorP)
    TextView tvOperatorP;
    @BindView(R.id.tvDocumentNoP)
    TextView tvDocumentNoP;
    @BindView(R.id.tvTransactionTimeP)
    TextView tvTransactionTimeP;
    @BindView(R.id.tvTransactionTypeP)
    TextView tvTransactionTypeP;
    @BindView(R.id.tvTransactionAmountP)
    TextView tvTransactionAmountP;

    @BindView(R.id.llCreditDeduction)
    LinearLayout llCreditDeduction;
    @BindView(R.id.llCreditDeductionP)
    LinearLayout llCreditDeductionP;
    @BindView(R.id.llActualPayment)
    LinearLayout llActualPayment;
    @BindView(R.id.llActualPaymentP)
    LinearLayout llActualPaymentP;
    @BindView(R.id.tvCreditDeduction)
    TextView tvCreditDeduction;
    @BindView(R.id.tvCreditDeductionP)
    TextView tvCreditDeductionP;
    @BindView(R.id.tvActualPayment)
    TextView tvActualPayment;
    @BindView(R.id.tvActualPaymentP)
    TextView tvActualPaymentP;


    @BindView(R.id.btnClose)
    Button btnClose;
    @BindView(R.id.btnPrint)
    Button btnPrint;
    private AidlPrinter printerModule;

    public PayDetailDialog(@NonNull Context context) {
        super(context, R.style.BottomDialog);
        mContext = context;
        initView();
    }

    private void initView() {
        View view = View.inflate(mContext, R.layout.dialog_pay_detail, null);

        setContentView(view);
        setCanceledOnTouchOutside(false);

        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = window.getAttributes();
                int width = SystemUtil.getScreenWidth((Activity) mContext);
//        int width = WindowManager.LayoutParams.MATCH_PARENT;
        int height = WindowManager.LayoutParams.WRAP_CONTENT;

        lp.width = width;
        lp.height = height;
        window.setAttributes(lp);

        ButterKnife.bind(this);
        setListener();
        llCreditDeduction.setVisibility(View.GONE);
        llCreditDeductionP.setVisibility(View.GONE);
        llActualPayment.setVisibility(View.GONE);
        llActualPaymentP.setVisibility(View.GONE);
    }

    private void setListener() {
        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        btnPrint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                print();
//                //TODO 重打印
//                if (SDUtil.isExistSD()) {
//                    DialogHelper.showLoading(mContext,"正在打印中");
//                    //view转bitmap并保存在本地
//                    String path = BitmapUtil.viewSaveToImage(llPrintContent, SDUtil.createDir("LiaoNingPos"), "print.PNG",true);
//                    AppHelper.callPrint((Activity) mContext, path);
//                }else{
//                    Toast.makeText(mContext,"打印失败，未检测到存储设备",Toast.LENGTH_SHORT).show();
//                }

            }
        });

    }

    private void print() {
        //TODO 重打印
        if (SDUtil.isExistSD() && printerModule != null) {
            try {
                Bitmap bitmap = BitmapUtil.createBitmap(llPrintContent);
                printerModule.printBitMap(0, bitmap, 30, new AidlPrinterListener.Stub() {
                    @Override
                    public void onError(int code, String mess) throws RemoteException {
                        DialogHelper.endLoading();
                        Toast.makeText(mContext,mess,Toast.LENGTH_SHORT).show();
                        Log.d("LOG", "打印失败：" + mess);//具体的code见ServiceResult
                    }

                    @Override
                    public void onFinish() throws RemoteException {
                        DialogHelper.endLoading();
                        printerModule.paperSkip(2);
                        Toast.makeText(mContext,"打印成功",Toast.LENGTH_SHORT).show();
                    }
                });
            } catch (RemoteException e) {
                e.printStackTrace();
                LogUtil.i("PayDetailDialog", "图片打印异常");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(mContext,"打印失败，未检测到存储设备",Toast.LENGTH_SHORT).show();
        }
    }

    public void setData(PayDetailMDL data) {
        if (data != null) {
            tvMerchantName.setText(data.getMerchantname());
            tvMerchantNo.setText(data.getMerchantno());
            tvTerminalNo.setText(data.getTerminalno());
            tvOperator.setText(data.getOperno());
            tvDocumentNo.setText(data.getTraceno());
            tvTransactionTime.setText(data.getDate() + " " + data.getTime());
            tvTransactionAmount.setText(StringUtils.divideHundred(data.getAmt()));

            tvMerchantNameP.setText(data.getMerchantname());
            tvMerchantNoP.setText(data.getMerchantno());
            tvTerminalNoP.setText(data.getTerminalno());
            tvOperatorP.setText(data.getOperno());
            tvDocumentNoP.setText(data.getTraceno());
            tvTransactionTimeP.setText(data.getDate() + " " + data.getTime());
            tvTransactionAmountP.setText(StringUtils.divideHundred(data.getAmt()));
            if ("1".equalsIgnoreCase(data.getType())) {
                tvTransactionType.setText("刷卡");
                tvTransactionTypeP.setText("刷卡");
            } else if ("2".equalsIgnoreCase(data.getType())) {
                tvTransactionType.setText("银联付款码");
                tvTransactionTypeP.setText("银联付款码");
            } else if ("3".equalsIgnoreCase(data.getType())) {
                tvTransactionType.setText("高速付款码");
                tvTransactionTypeP.setText("高速付款码");
            }else if ("4".equalsIgnoreCase(data.getType())) {
                tvTransactionType.setText("微信付款码");
                tvTransactionTypeP.setText("微信付款码");
            }else if ("5".equalsIgnoreCase(data.getType())) {
                tvTransactionType.setText("支付宝付款码");
                tvTransactionTypeP.setText("支付宝付款码");
            }

        }
    }

    public void setPrintModule(AidlPrinter printerModule) {
        this.printerModule = printerModule;
    }
}
